import { Node, UIOpacity, Vec3 } from "cc";
import { ViewUtil } from "db://app-game/core/utils/ViewUtil";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { StaticModelComp } from "./model/StaticModelComp";
import { StaticCreatureViewComp } from "./view/StaticCreatureViewComp";

/** 静态生物实例 */
@ecs.register("StaticCreature")
export class StaticCreature extends ecs.Entity {
  // 数据层
  StaticModel!: StaticModelComp;

  // 视图层
  StaticCreatureView!: StaticCreatureViewComp;
  init() {
    this.add(StaticModelComp);
  }

  /** 加载静态生物 */
  load(parent: Node, pos: Vec3) {
    // 加载节点
    const spriteFrame = this.StaticModel.data.spriteFrame;
    const node = ViewUtil.createSpriteNode(spriteFrame);
    node.addComponent(UIOpacity);
    const StaticCreatureView = node.addComponent(StaticCreatureViewComp);
    this.add(StaticCreatureView);
    node.setPosition(pos);
    node.parent = parent;
    this.StaticModel.node = node;

    // 2.添加组件
  }

  /** 设置透明 */
  setTransparent(status: boolean) {
    const uiOpacity = this.StaticCreatureView.node.getComponent(UIOpacity);
    if (status) {
      uiOpacity.opacity = 50;
    } else {
      uiOpacity.opacity = 255;
    }
  }
}
